package model.structs;

import java.util.Hashtable;

/**
 * Maintains the set of locations that exist at certain coordinates of a map.
 *
 * @author rhaq
 * @version 1.00 2009-05-09 Initial submission.
 */
public class LocationTable extends Hashtable
{
	/** A temporary coordinate object used to conserve memory when querying the table. */
	private Point tempPoint;

	
	/**
	 * Constructs the table so that location data may be inserted.
	 */
	public LocationTable()
	{
		super();
		this.tempPoint = new Point();
	}

	
	/**
	 * Constructs the table with the specified initial capacity so that location data
	 * may be inserted.
	 * @param initialCapacity The initial amount of entries to allow for insertion.
	 */
	public LocationTable(int initialCapacity)
	{
		super(initialCapacity);
	}


	/**
	 * Retrieves the location that exists at the specified coordinates.
	 * @param x The x-coordinate on the map where the location may exist.
	 * @param y The y-coordinate on the map where the location may exist.
	 * @return The location that exists at the specified coordinates, null if no locations exist
	 * at the specified location.
	 */
	public synchronized Location get(int x, int y)
	{
		tempPoint.setX(x);
		tempPoint.setY(y);

		return (Location)super.get(tempPoint);
	}


	/**
	 * Inserts the specified location into the table at the specified coordinates.
	 * @param x The x-coordinate to insert the location.
	 * @param y The y-coordinate to insert the location.
	 * @param l The location to insert at the specified coordinates. An actual operation will only
	 * be performed if this value is not null.
	 */
	public void put(int x, int y, Location l)
	{
		if (l != null)
			this.put( new Point(x, y) , l);
	}
}